package com.dc.utils;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;

import java.util.List;
import java.util.Map;

public class JsonUtils {

    private static final Gson gson = new Gson();

    public static <T> T fromJson(String jsonStr, Class<T> clazz){
        return gson.fromJson(jsonStr, clazz);
    }

    public static String toJson(Object object){
        return gson.toJson(object);
    }

    public static String toJson(JsonObject jsonObject){
        return gson.toJson(jsonObject);
    }

    public static Map<String,Object> getJsonMap(String jsonStr){
        return gson.fromJson(jsonStr, Map.class);
    }


    public static  <T> List<T> toList(String json, Class<T> clazz) {
        return gson.fromJson(json, TypeToken.getParameterized(List.class, clazz).getType());
    }

}
